package com.cosmic.pizza.web.servlet;

import com.cosmic.pizza.pojo.Address;
import com.cosmic.pizza.pojo.PageBean;
import com.cosmic.pizza.pojo.ResultInfo;
import com.cosmic.pizza.pojo.User;
import com.cosmic.pizza.service.AddressService;
import com.cosmic.pizza.service.Impl.AddressServiceImpl;
import com.cosmic.pizza.util.Md5Util;
import com.cosmic.pizza.web.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/Address/*")
public class AddressServlet extends BaseServlet {
    private AddressService addressService = new AddressServiceImpl();

    public void findMyAllAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object _user = req.getSession().getAttribute("user");
        ResultInfo resultInfo = new ResultInfo();
        User user = (User) _user;
        if (user == null) {
            resultInfo.setFlag(false);
        } else {
            int pageSize = 4;
            int currentPage = 1;
            String _currentPage = req.getParameter("currentPage");
            if (_currentPage != null && _currentPage.length() > 0) {
                currentPage = Integer.parseInt(_currentPage);
            }
            PageBean<Address> pb = addressService.findAddress(user.getUid(), pageSize, currentPage);
            resultInfo.setData(pb);
        }
        writeValue(resp, resultInfo);
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object _user = req.getSession().getAttribute("user");
        ResultInfo resultInfo = new ResultInfo();
        User user = (User) _user;
        if (user == null) {
            resultInfo.setFlag(false);
        } else {
            List<Address> list = addressService.findAddress(user.getUid());
            resultInfo.setFlag(true);
            resultInfo.setData(list);
        }
        writeValue(resp, resultInfo);
    }

    public void addMyAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object _user = req.getSession().getAttribute("user");
        ResultInfo resultInfo = new ResultInfo();
        User user = (User) _user;
        if (user == null) {
            resultInfo.setFlag(false);
            System.out.println("用户不存在");
        } else {
            Address address = new Address();
            address.setName(req.getParameter("name"));
            address.setTelephone(req.getParameter("telephone"));
            address.setSex(req.getParameter("sex"));
            address.setAddress_detail(req.getParameter("address"));
            boolean flag = addressService.addAddress(user.getUid(), address);
            resultInfo.setFlag(flag);
        }
        writeValue(resp, resultInfo);
    }

    public void findDefaultAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object _user = req.getSession().getAttribute("user");
        User user = (User) _user;
        Address address = null;
        if (user != null) {
            address = addressService.findFirstAddress(user.getUid());
        }
        writeValue(resp, address);
    }

    public void findCheckedAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String _add_id = req.getParameter("add_id");
        Integer add_id = Integer.parseInt(_add_id);
        Address address = addressService.findAddressByaid(add_id);
        writeValue(resp, address);
    }

    public void updateAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Address address = new Address();
        ResultInfo info = new ResultInfo();
        String address_detail = req.getParameter("address");
        address.setName(req.getParameter("name"));
        address.setAdd_id(Integer.parseInt(req.getParameter("id")));
        address.setTelephone(req.getParameter("telephone"));
        address.setSex(req.getParameter("sex"));
        address.setAddress_detail(address_detail);
        boolean flag = addressService.update(address);
        info.setFlag(flag);
        writeValue(resp, info);
    }

    public void deleteAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String aid = req.getParameter("aid");
        int add_id = Integer.parseInt(aid);
        boolean flag = addressService.delete(add_id);
        ResultInfo info = new ResultInfo();
        info.setFlag(flag);
        writeValue(resp, info);
    }

    public void setDefaultAddress(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String aid = req.getParameter("aid");
        int add_id = Integer.parseInt(aid);
        boolean flag = addressService.setDefault(add_id);
        ResultInfo info = new ResultInfo();
        info.setFlag(flag);
        writeValue(resp, info);
    }

}
